package com.vodden.money.server;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.vodden.money.shared.Account;
import com.vodden.money.shared.Entry;
import com.vodden.money.shared.EntryGroup;

public class AccountTestServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		Entry firstJournalEntry = null;
		Entry secondJournalEntry = null;
		Date postDate;
		Account firstAccount;
		Account secondAccount;
		EntryGroup entryGroup = new EntryGroup();

		// Set up some test things and print bits of them out

		try {
			response.setContentType("text/plain");
			response.getWriter().println("This is a test");

			firstAccount = new Account("1st Account",
					"This is our First Account");
			secondAccount = new Account("2nd Account",
					"This is our Second Account");

			postDate = new Date();
			firstJournalEntry = new Entry(postDate, firstAccount, 10,
					"This is a test");
			secondJournalEntry = new Entry(postDate, secondAccount, -10,
					"This is a test number 2");

			response.getWriter().println(
					"Date  : " + firstJournalEntry.getDate());
			response.getWriter().println(
					"Value : " + firstJournalEntry.getValue());

			response.getWriter().println(
					"Date  : " + secondJournalEntry.getDate());
			response.getWriter().println(
					"Value : " + secondJournalEntry.getValue());

			entryGroup.add(firstJournalEntry);
			entryGroup.add(secondJournalEntry);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// Save the things and close the PersistenceManager.

		PersistenceManager pm = PMF.get().getPersistenceManager();

		try {
			response.getWriter().println("Testing Save");
			pm.makePersistent(entryGroup);
		} catch (IOException exception) {
			exception.printStackTrace();
		} finally {
			pm.close();
		}

		// Open another persistence manager and try and get one of the things
		// back.

		pm = PMF.get().getPersistenceManager();

		Query query = pm.newQuery("select from " + EntryGroup.class.getName());
		@SuppressWarnings("unchecked")
		List<EntryGroup> results = (List<EntryGroup>) query.execute();

		try {
			response.getWriter().println("Testing persistence:");
			for (EntryGroup result : results) {
				for (Entry entryOne : result) {
					try {
						response.getWriter().println(
								"Date  : " + entryOne.getDate());
						response.getWriter().println(
								"Value : " + entryOne.getValue());
						response.getWriter().println(
								"Descr : " + entryOne.getDescription());
					} catch (NullPointerException exception) {
						exception.printStackTrace();
					}
				}
			}
		} catch (IOException exception) {
			exception.printStackTrace();
		}

		pm.close();
	}
}
